﻿@model OrderModel.AddOrderProductModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId);
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-sales.png")" alt="" />
        @string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId) @Html.ActionLink("(" + T("Admin.Orders.Products.AddNew.BackToOrder") + ")", "Edit", new { id = Model.OrderId })
    </div>
    <div class="options">
    </div>
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchProductName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.SearchProductName)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchCategoryId):
        </td>
        <td class="adminData">
            @Html.DropDownList("SearchCategoryId", Model.AvailableCategories)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchManufacturerId):
        </td>
        <td class="adminData">
            @Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchProductTypeId):
        </td>
        <td class="adminData">
            @Html.DropDownList("SearchProductTypeId", Model.AvailableProductTypes)
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="search-products" class="k-button" value="@T("Admin.Common.Search")" />
        </td>
    </tr>
</table>
<table class="adminContent">
    <tr>
        <td>
            <strong>@T("Admin.Orders.Products.AddNew.Note1")</strong>
        </td>
    </tr>
    <tr>
        <td>
            <div id="products-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#products-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("AddProductToOrder", "Order"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)]
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        //selectable
                        selectable: true,
                        change: onChange,
                        columns: [{
                            field: "Id",
                            title: "ID",
                            width: 50
                        }, {
                            field: "Name",
                            title: "@T("Admin.Orders.Products.AddNew.Name")",
                            width: 400
                        },{
                            field: "Sku",
                            title: "@T("Admin.Orders.Products.AddNew.SKU")",
                            width: 100
                        }]
                    });
                });
            </script>
            <script type="text/javascript">
                $(document).ready(function () {
                    $('#search-products').click(function () {
                        var grid = $('#products-grid').data('kendoGrid');
                        grid.dataSource.page(1); //new search. Set page size to 1
                        grid.dataSource.read();
                        return false;
                    });
                });

                $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
                    if (event.keyCode == 13) {
                        $("#search-products").click();
                        return false;
                    }
                });

                function additionalData() {
                    return {
                        SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                        SearchCategoryId: $('#SearchCategoryId').val(),
                        SearchManufacturerId: $('#SearchManufacturerId').val(),
                        SearchProductTypeId: $('#SearchProductTypeId').val()
                    };
                }

                function onChange(e) {
                    var grid = $('#products-grid').data('kendoGrid');
                    var selectedItem = grid.dataItem(grid.select());
                    var productId = selectedItem.Id;
                    //load "product details page" block
                    var productDetailsActionUrl = '@Html.Raw(Url.Action("AddProductToOrderDetails", "Order", new { orderId = Model.OrderId, productId = "productidplaceholder" }))';
                    productDetailsActionUrl = productDetailsActionUrl.replace("productidplaceholder", productId);
                    setLocation(productDetailsActionUrl);
                }
            </script>
        </td>
    </tr>
</table>
<div id="productdetails">
</div>
@Html.HiddenFor(model => model.OrderId)
